package com.dc.project.util.reader.base;

import lombok.Data;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * FileReaderModel
 *
 * @author chenxueli
 * @date 2025-03-25 16:06:00
 */
@Data
public class FileReaderModel implements Serializable {

    public static final FileReaderModel EMPTY = FileReaderModel.newInstance("");
    /**
     * 文件主要内容
     */
    private String content;
    /**
     * 子文件列表
     */
    private List<File> subFileList;

    public static FileReaderModel newInstance(String content) {
        var model = new FileReaderModel();
        model.setContent(content);
        return model;
    }

    public static FileReaderModel newInstance(String content, List<File> fileList) {
        var model = new FileReaderModel();
        model.setContent(content);
        model.setSubFileList(fileList);
        return model;
    }

    /**
     * 添加子文件到列表中
     *
     * @param file 子文件
     */
    public void put(File file) {
        if (subFileList == null) {
            subFileList = new ArrayList<>();
        }
        subFileList.add(file);
    }

    /**
     * 添加子文件到列表中
     *
     * @param fileList 子文件
     */
    public void put(List<File> fileList) {
        if (subFileList == null) {
            subFileList = new ArrayList<>();
        }
        subFileList.addAll(fileList);
    }

    /**
     * 获取占位符
     *
     * @param i 占位符索引
     * @return 占位符字符串
     */
    public static String getPlaceholder(int i) {
        return "{placeholder{" + i + "}}";
    }

    /**
     * 获取占位符
     *
     * @param i 占位符索引
     * @return 占位符字符串
     */
    public static String replacePlaceholder(int i, String content, String replaceContent) {
        return content.replace(getPlaceholder(i), replaceContent);
    }

    /**
     * 清除占位符
     *
     * @param content 内容
     * @return 清除后的内容
     */
    public static String clearPlaceholder(String content) {
        return content.replaceAll("\\{placeholder\\{\\d+}}", "");
    }

}
